﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAnB
{
    public partial class PIN_login : Form
    {
        private DataTable dt = new DataTable();
        public PIN_login()
        {
            InitializeComponent();
           
            textBox5.Visible = false;
            btnOK.Visible = false;
                


            PinCode(textBox1);
            PinCode(textBox2);
            PinCode(textBox3);
            PinCode(textBox4);
            

        }
        void PinCode(TextBox x)
        {
            x.UseSystemPasswordChar = true;
            x.MaxLength = 1;
            x.Font = new Font(x.Font.FontFamily, 28);
            x.Size = new System.Drawing.Size(50, 50);
            x.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;



        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox2.Focus();
            if (textBox1.Text == String.Empty)
                textBox1.Focus();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox3.Focus();
            if (textBox2.Text == String.Empty)
                textBox1.Focus();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            textBox4.Focus();
            if (textBox3.Text == String.Empty)
                textBox2.Focus();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text == String.Empty)
                textBox3.Focus();
            if(textBox1.Text != String.Empty &&textBox2.Text != String.Empty &&textBox3.Text != String.Empty &&textBox4.Text != String.Empty){
            ketnoi kn = new ketnoi();
            string pin = textBox1.Text + textBox2.Text + textBox3.Text + textBox4.Text;
            string sql = "select * from NguoiDung Where PIN ='"+pin+"'";
            dt = kn.getDataTable(sql);
           
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("PIN khong dung");
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox1.Focus();
            }
            else
            {
                
                if ((bool)dt.Rows[0]["isAdmin"] == true)
                {
                    lblPIN.Text = "Mật khẩu quản lý?";
                    btnOK.Visible = true;
                    textBox5.Visible = true;
                    textBox5.Focus();


                    MainAdmin f = new MainAdmin();

                    this.Hide();
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        this.Show();
                        textBox1.Clear();
                        textBox2.Clear();
                        textBox3.Clear();
                        textBox4.Clear();
                        textBox1.Focus();
                        textBox5.Visible = false;
                        textBox5.Clear();
                        btnOK.Visible = false;
                        lblPIN.Text = "Nhập PIN !!!!!";
                    }

                }
                else
                {
                    Session.s((int)dt.Rows[0]["MaNV"], dt.Rows[0]["TenNV"].ToString(), false);
                    Main f = new Main();
                    this.Hide();
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        this.Show();
                        textBox1.Clear();
                        textBox2.Clear();
                        textBox3.Clear();
                        textBox4.Clear();
                        textBox1.Focus();
                    }
                    
                    
                }
             
            }
            }
            

        }

        private void PIN_login_Shown(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (textBox5.Text != dt.Rows[0]["MatKhau"].ToString())
            {
                MessageBox.Show("sai");
                textBox5.Clear();

            }
            else 
            {
                //MessageBox.Show("OKkkkkk");
                MainAdmin f = new MainAdmin();
                
                this.Hide();
                if (f.ShowDialog() == DialogResult.Yes)
                {
                    this.Show();
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox4.Clear();
                    textBox1.Focus();
                    textBox5.Visible = false;
                    textBox5.Clear();
                    btnOK.Visible = false;
                    lblPIN.Text = "Nhập PIN !!!!!";
                }
            
            }
                
            
            
        }

        private void đăngKíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDangKiNV f = new frmDangKiNV();
            f.ShowDialog();
        }

      

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout f = new frmAbout();
            f.ShowDialog();
        }


        private void hướngDẫnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("C:\\Users\\Azure\\Desktop\\huongdan.docx");
      
            
        }

        private void quênPINToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmQuenPin f = new frmQuenPin();
            f.ShowDialog();
        }

        private void liênHệToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmLienHe f = new frmLienHe();
            f.ShowDialog();
        }

    }
}
